package com.sarnath.sardoop.modules.hive.web;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sarnath.sardoop.common.bean.Response;
import com.sarnath.sardoop.common.utils.StringUtils;
import com.sarnath.sardoop.common.web.BaseController;
import com.sarnath.sardoop.modules.hive.service.HiveSqlService;

/**
 * @author yingc
 * Hive Sql 执行接口
 */
@Controller
@RequestMapping(value = "${adminPath}/hive")
public class HiveSqlController extends BaseController {
	@Resource
	HiveSqlService hiveSqlService;
	
	/**
	 * 单行单列数据查询
	 * @return
	 */
	@RequestMapping(value="queryForObject")
	@ResponseBody
	public Response queryForObject(@RequestParam String sql){
		Response res=new Response();
		try {
			res.put("result",hiveSqlService.queryForObject(sql));
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	/**
	 * 单行数据查询
	 * @return
	 */
	@RequestMapping(value="queryForMap")
	@ResponseBody
	public Response queryForMap(@RequestParam String sql){
		Response res=new Response();
		try {
			res.put("result",hiveSqlService.queryForMap(sql));
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	/**
	 * 多行数据查询
	 * @return
	 */
	@RequestMapping(value="queryForList")
	@ResponseBody
	public Response queryForList(@RequestParam String sql){
		Response res=new Response();
		try {
			res.put("result",hiveSqlService.queryForList(sql));
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	/**
	 * show tables
	 * @return
	 */
	@RequestMapping(value="tables")
	@ResponseBody
	public Response tables(@RequestParam(required=false) String database){
		Response res=new Response();
		try {
			if(StringUtils.isBlank(database)){
				database="default";
			}
			res.put("result",hiveSqlService.queryForList("show tables in "+database));
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	/**
	 * 执行SQL语句
	 * @return
	 */
	@RequestMapping(value="execute")
	@ResponseBody
	public Response execute(@RequestParam String sql){
		Response res=new Response();
		try {
			hiveSqlService.execute(sql);
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	
	/**
	 * 调用webHcat执行SQL语句
	 * @return
	 */
	@RequestMapping(value="job")
	@ResponseBody
	public Response job(@RequestParam String sql,@RequestParam String path){
		Response res=new Response();
		try {
			Map job = hiveSqlService.job(sql,path);
			if(job.get("id")!=null){
				res.put("id", job.get("id"));
			}else{
				res.setStatus(1);
				res.setMsg("执行失败！");
			}
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
}
